package com.iotp.utils.exception;

import com.iotp.entity.base.DataMessage;
import com.iotp.entity.base.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

/**
 * 全局异常类
 */
@CrossOrigin
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler
    public DataMessage processException(Exception ex, HttpServletRequest request, HttpServletResponse response, Object handler){
        if (!(request.getHeader("accept").contains("application/json") || (request.getHeader("X-Requested-With") != null
                && request.getHeader("X-Requested-With").contains("XMLHttpRequest")))) {
            StackTraceElement[] stes = ex.getStackTrace();
            long exceptionSeq = System.currentTimeMillis();
            logger.error("GlobalException----------" + exceptionSeq + "-----Begin----------");
            for (StackTraceElement ste : stes) {
                String className = ste.getClassName();
                if (className != null && className.indexOf("iotp.") != -1) {
                    logger.error("请求异常-" + "异常名称：" + ex.getClass().getName() + "，所属文件：" + ste.getFileName() + "，所属类："
                            + ste.getClassName() + "，调用方法：" + ste.getMethodName() + "，所在代码行：" + ste.getLineNumber());
                }
            }
            logger.error(ex.getMessage());
            logger.error("GlobalException----------" + exceptionSeq + "-----End------------\n");
        } else {
            StackTraceElement[] stes = ex.getStackTrace();
            long exceptionSeq = System.currentTimeMillis();
            logger.error("GlobalException----------" + exceptionSeq + "-----Begin----------");
            for (StackTraceElement ste : stes) {
                String className = ste.getClassName();
                if (className != null && className.indexOf("iotp.") != -1) {
                    logger.error("Ajax请求异常-" + "异常名称：" + ex.getClass().getName() + "，所属文件：" + ste.getFileName() + "，所属类："
                            + ste.getClassName() + "，调用方法：" + ste.getMethodName() + "，所在代码行：" + ste.getLineNumber());
                }
            }
            logger.error(ex.getMessage());
            logger.error("GlobalException----------" + exceptionSeq + "-----End------------\n");
        }

        ex.printStackTrace();
        if(ex instanceof MissingServletRequestParameterException){
//            return new ResponseResult(400, ex.getMessage(), null);
            return new DataMessage(400, null, ex.getMessage());
        }
        if(ex instanceof NoFoundExcepiton){
            return new DataMessage(401, null, "无法找到相应数据！");
        }
        return new DataMessage(500, null, ex.getMessage());
    }



}
